<!DOCTYPE html>




<html class="theme-next gemini" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.3">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="Mock Server,MyMockServer,Proxy," />





  <link rel="alternate" href="/atom.xml" title="流水不腐小夏" type="application/atom+xml" />






<meta name="description" content="使用MyMockServer模拟数据   使用MyMockServer模拟数据 简介 安装 使用 描述 请求匹配 响应信息 控制信息   示例 相关资料     简介在开发的时候，前端、移动端、后端开发分离，开发流程是先定义好接口API，然后按照接口约定进行开发。所以经常需要根据接口Mock数据，有从代码层进行Mock，也有从网络层进行代理的。 本人使用方式是在网络代理进行Mock，因为在代码层M">
<meta name="keywords" content="Mock Server,MyMockServer,Proxy">
<meta property="og:type" content="article">
<meta property="og:title" content="使用MyMockServer模拟数据">
<meta property="og:url" content="http://mycommons.cn/2017/10/13/MyMockServer/index.html">
<meta property="og:site_name" content="流水不腐小夏">
<meta property="og:description" content="使用MyMockServer模拟数据   使用MyMockServer模拟数据 简介 安装 使用 描述 请求匹配 响应信息 控制信息   示例 相关资料     简介在开发的时候，前端、移动端、后端开发分离，开发流程是先定义好接口API，然后按照接口约定进行开发。所以经常需要根据接口Mock数据，有从代码层进行Mock，也有从网络层进行代理的。 本人使用方式是在网络代理进行Mock，因为在代码层M">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/1520343-63e619ea0a5b37a7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2017-10-25T11:46:41.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="使用MyMockServer模拟数据">
<meta name="twitter:description" content="使用MyMockServer模拟数据   使用MyMockServer模拟数据 简介 安装 使用 描述 请求匹配 响应信息 控制信息   示例 相关资料     简介在开发的时候，前端、移动端、后端开发分离，开发流程是先定义好接口API，然后按照接口约定进行开发。所以经常需要根据接口Mock数据，有从代码层进行Mock，也有从网络层进行代理的。 本人使用方式是在网络代理进行Mock，因为在代码层M">
<meta name="twitter:image" content="http://upload-images.jianshu.io/upload_images/1520343-63e619ea0a5b37a7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.3',
    sidebar: {"position":"left","display":"always","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":false,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: 'GRLHBBCD9E',
      apiKey: '759ab3f5c09b132b3ed4842a44c53c45',
      indexName: 'mycommons',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://mycommons.cn/2017/10/13/MyMockServer/"/>





  <title>使用MyMockServer模拟数据 | 流水不腐小夏</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?55458ffca1f60dd283f11b9a7f82b262";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">流水不腐小夏</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">http://mycommons.cn</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  
  <div class="algolia-popup popup search-popup">
    <div class="algolia-search">
      <div class="algolia-search-input-icon">
        <i class="fa fa-search"></i>
      </div>
      <div class="algolia-search-input" id="algolia-search-input"></div>
    </div>

    <div class="algolia-results">
      <div id="algolia-stats"></div>
      <div id="algolia-hits"></div>
      <div id="algolia-pagination" class="algolia-pagination"></div>
    </div>

    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
  </div>




    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://mycommons.cn/2017/10/13/MyMockServer/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="流水不腐小夏">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="流水不腐小夏">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">使用MyMockServer模拟数据</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-10-13T18:34:12+08:00">
                2017-10-13
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/10/13/MyMockServer/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2017/10/13/MyMockServer/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv">本文总阅读量
            <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>次
            </span>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="使用MyMockServer模拟数据"><a href="#使用MyMockServer模拟数据" class="headerlink" title="使用MyMockServer模拟数据"></a>使用MyMockServer模拟数据</h1><hr>
<!-- TOC -->
<ul>
<li><a href="#使用mymockserver模拟数据">使用MyMockServer模拟数据</a><ul>
<li><a href="#简介">简介</a></li>
<li><a href="#安装">安装</a></li>
<li><a href="#使用">使用</a><ul>
<li><a href="#描述">描述</a></li>
<li><a href="#请求匹配">请求匹配</a></li>
<li><a href="#响应信息">响应信息</a></li>
<li><a href="#控制信息">控制信息</a></li>
</ul>
</li>
<li><a href="#示例">示例</a></li>
<li><a href="#相关资料">相关资料</a></li>
</ul>
</li>
</ul>
<!-- /TOC -->
<h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><p>在开发的时候，前端、移动端、后端开发分离，开发流程是先定义好接口API，然后按照接口约定进行开发。所以经常需要根据接口Mock数据，有从代码层进行Mock，也有从网络层进行代理的。</p>
<p>本人使用方式是在网络代理进行Mock，因为在代码层Mock，数据不是很灵活，如果要改变数据，那么需要重新编译再启动（本人是移动开发），浪费不少时间。</p>
<p>在网络层Mock，使用的软件是Charles，然后对需要Mock的数据进行mapping就行了，可以是一个远程接口，也可以是一个本地文件。关于Charles使用，这里有一篇文章<a href="http://blog.csdn.net/guijiaoba/article/details/43198503" target="_blank" rel="external">介绍</a>。</p>
<p>上面的Mcok方式已经足够友好了，基本上可以满足90%的日常开发，但是还有些数据是不能满足的，比如服务器需要返回一个动态的时间戳，这个用刚刚的方式就不行。所以很多开发者自己启动一个服务，然后返回对应的数据，这样也是可以的，但是有几个缺点。</p>
<ul>
<li><p>自己启动服务，需要重新映射修改域名，同时对开发者要求也比较高，需要有服务器开发相关知识。</p>
</li>
<li><p>如果只想部分接口Mock数据，部分接口走真是数据，就需要修改代码或者做些配置，这样代码耦合性太高或者配置麻烦</p>
</li>
</ul>
<p>于是世面出现了很多使用脚本实现一个代理服务器，对所有的接口进行过滤，添加一些白名单功能，对特定的接口返回特定的数据。常见的如阿里巴巴开源的anyproxy。MyMockServer也是这样的，作为用代理服务器实现的Mock功能的服务器，以后简称mms，不过相对于使用脚本实现代理服务器，mms主要优势在于：</p>
<ul>
<li><p>使用DSL配置，相对于json配置，最大优势，简单易懂，用户体验会更好</p>
</li>
<li><p>在DSL中可以使用Groovy语言修改相关逻辑处理，动态的返回数据</p>
</li>
<li><p>动态监听配置文件，修改文件后，立马生效，不需要重启服务，节省时间</p>
</li>
</ul>
<h2 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h2><p>使用Mac HomeBrew安装</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">// add tap</div><div class="line">brew tap LiushuiXiaoxia/mms</div><div class="line"></div><div class="line">// brew install</div><div class="line">brew install mms</div></pre></td></tr></table></figure>
<p>其他平台直接下载压缩包即可，然后把压缩包中的bin目录添加到系统环境变量中，然后直接调用<code>MyMockServer</code>或者<code>MyMockServer.bat</code>。</p>
<h2 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h2><p>可以直接使用<code>mms</code>即可启动服务，<code>mms</code>支持参数如下。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">$ mms -h</div><div class="line">Path: /usr/<span class="built_in">local</span>/bin/.</div><div class="line">Cmd: mms -h</div><div class="line">Log level : INFO</div><div class="line">usage: mms [options]</div><div class="line"> -c,--config &lt;arg&gt;   mock server path, default is current directory</div><div class="line"> -h,--<span class="built_in">help</span>           output usage information</div><div class="line"> -i,--init           init some path as mock workspace</div><div class="line"> -l,--level &lt;arg&gt;    <span class="built_in">log</span> level ALL,DEBUG,INFO,WARN,ERROR,FATAL,OFF,TRACE</div><div class="line"> -p,--port &lt;arg&gt;     mock server port, default is 9090</div></pre></td></tr></table></figure>
<p>首先定义一个工作目录，默认情况下使用当前目录作为工作目录，工作目录用于保存配置文件。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">$ mkdir proxy</div><div class="line"></div><div class="line">$ <span class="built_in">cd</span> proxy</div><div class="line"></div><div class="line">$ mms -i</div><div class="line">Path: /Users/xiaqiulei/proxy/.</div><div class="line">Cmd: mms -i</div><div class="line">Log level : INFO</div><div class="line"></div><div class="line">$ ls</div><div class="line">template.groovy</div></pre></td></tr></table></figure>
<p>当然了也可以手动指定工作目录，这两种方式一样。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">$ mkdir proxy</div><div class="line"></div><div class="line">$ mms -i -c ./proxy</div><div class="line">Path: /Users/xiaqiulei/<span class="built_in">test</span>/.</div><div class="line">Cmd: mms -i -c ./proxy</div><div class="line">Log level : INFO</div><div class="line"></div><div class="line">$ ls proxy</div><div class="line">template.groovy</div></pre></td></tr></table></figure>
<p>在工作目录中会自动生成模板文件<code>template.groovy</code>。</p>
<p>其实这个代理服务器使用的就是用Groovy实现的DSL功能。</p>
<figure class="highlight groovy"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div></pre></td><td class="code"><pre><div class="line">mock &#123;</div><div class="line"><span class="comment">//    enable true</span></div><div class="line">    desc <span class="string">"template"</span></div><div class="line">    request &#123;</div><div class="line"><span class="comment">//        scheme "http"</span></div><div class="line">        host <span class="string">"www.google.com"</span></div><div class="line"><span class="comment">//        port 80</span></div><div class="line"><span class="comment">//        path "/api/login/"</span></div><div class="line">        method <span class="string">"GET"</span></div><div class="line">        params &#123;</div><div class="line"><span class="comment">//            param "param_key_1", "param_value_1"</span></div><div class="line"><span class="comment">//            param "param_key_2", "param_value_2"</span></div><div class="line">        &#125;</div><div class="line">        headers &#123;</div><div class="line">            header <span class="string">"header_key_1"</span>, <span class="string">"header_value_1"</span></div><div class="line">            header <span class="string">"time"</span>, <span class="keyword">new</span> Date().format(<span class="string">"yyyy-MM-dd HH:mm:ss"</span>)</div><div class="line">        &#125;</div><div class="line">        body &#123;</div><div class="line"></div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    response &#123;</div><div class="line"><span class="comment">//        version 'HTTP/1.1'</span></div><div class="line"><span class="comment">//        code 200</span></div><div class="line">        headers &#123;</div><div class="line">            header <span class="string">"header_key_1"</span>, <span class="string">"header_value_1"</span></div><div class="line">            header <span class="string">"time"</span>, <span class="keyword">new</span> Date().format(<span class="string">"yyyy-MM-dd HH:mm:ss"</span>)</div><div class="line">        &#125;</div><div class="line">        body &#123;</div><div class="line"><span class="comment">//            text "text"</span></div><div class="line"><span class="comment">//            textFile "file.txt"</span></div><div class="line"><span class="comment">//</span></div><div class="line">            json <span class="string">"""&#123;"key":"value"&#125;"""</span></div><div class="line"><span class="comment">//            jsonFile "file.txt"</span></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="comment">//            xml "&lt;xml/&gt;"</span></div><div class="line"><span class="comment">//            xmlFile "file.txt"</span></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="comment">//            html "&lt;html/&gt;"</span></div><div class="line"><span class="comment">//            htmlFile "file.txt"</span></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="comment">//            file "file.txt"</span></div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"><span class="comment">//    control &#123;</span></div><div class="line"><span class="comment">//        delay 3</span></div><div class="line"><span class="comment">//    &#125;</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>做Android的同学，看到这个是不是眼熟，跟咱们的gradle是不是很像，没错，gradle也是一种DSL。</p>
<p>下面依次做讲解，mock下面主要分为4部分：描述，请求匹配、响应信息、控制部分。</p>
<h3 id="描述"><a href="#描述" class="headerlink" title="描述"></a>描述</h3><p><strong>enable</strong>: 是否启用本条mock配置，默认是true，如果需要取消本次配置，可以设置为<code>enable false</code></p>
<p><strong>desc</strong>: 本次mock配置的描述信息</p>
<h3 id="请求匹配"><a href="#请求匹配" class="headerlink" title="请求匹配"></a>请求匹配</h3><p><strong>scheme</strong>: 请求scheme，如果指定某一种，则只匹配这种，如果不设置，则默认为<code>http</code>和<code>https</code>都匹配</p>
<p><strong>host</strong>: 主机类型，如果设置，则匹配host，否则为null，则认为匹配成功</p>
<p><strong>port</strong>: 请求端口，如果设置，则匹配端口，否则为80</p>
<p><strong>path</strong>: 请求路径，如果设置，则匹配路径，否则为null，则认为匹配成功</p>
<p><strong>method</strong>: 请求方法，如果设置，则匹配方法，否则为null，则认为匹配全部方法</p>
<p><strong>params</strong>: 请求参数，如果设置，则匹配参数，否则为null，则认为匹配成功，暂时无用，保留</p>
<p><strong>headers</strong>: 请求头部，如果设置，则匹配头部，否则为null，则认为匹配成功，暂时无用，保留</p>
<p><strong>body</strong>: 请求实体，如果设置，则匹配实体，否则为null，则认为匹配成功，暂时无用，保留</p>
<p>简单来说，如果设置了，就严格匹配，不设置则认为匹配成功。</p>
<h3 id="响应信息"><a href="#响应信息" class="headerlink" title="响应信息"></a>响应信息</h3><p>同上面类似，只不过是返回的数据，如果request完全匹配，则返回response中的信息。</p>
<p><strong>version</strong>: http版本协议，默认为’HTTP/1.1’</p>
<p><strong>code</strong>: 响应状态吗，默认为200</p>
<p><strong>headers</strong>: 所有的头部</p>
<p><strong>header</strong>: 添加头部，一共2个参数，第一个为key，第二个为value</p>
<p><strong>body</strong>: 响应body</p>
<p><strong>text</strong>: 设置body为文本内容，参数为字符串类型</p>
<p><strong>textFile</strong>: 设置body为文本文件内容，参数为文本文件对于的路径</p>
<p><strong>json</strong>: 设置body为json，参数为json字符串</p>
<p><strong>jsonFile</strong> 设置body为json文件中的内容，参数为json文件路径</p>
<p><strong>xml</strong>: 设置body为xml，参数为xml字符串</p>
<p><strong>xmlFile</strong> 设置body为ml文件中内容，参数为xml文件路径</p>
<p><strong>html</strong>: 设置body为hml，参数为hml字符串</p>
<p><strong>htmlFile</strong>: 设置body为hml文件中内容，参数为html文件路径</p>
<p><strong>file</strong>: 设置body为一个文件的内容，参数为文件路径</p>
<p>因为配置文件使用的是Groovy，所以可以使用三个双引号来设置字符串，比如设置返回为json字符，json是是包含双引号的，如果按照Java中方式，那么就会出现转码，这样不方便阅读和修改。</p>
<p>那么可以这样设置，是不是很方便。</p>
<figure class="highlight gradle"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">json <span class="string">""</span><span class="string">"</span></div><div class="line"><span class="string">&#123;</span></div><div class="line"><span class="string">    "</span>key<span class="string">":"</span>value<span class="string">",</span></div><div class="line"><span class="string">    "</span>hello<span class="string">":"</span>world<span class="string">"</span></div><div class="line"><span class="string">&#125;</span></div><div class="line"><span class="string">"</span><span class="string">""</span></div></pre></td></tr></table></figure>
<h3 id="控制信息"><a href="#控制信息" class="headerlink" title="控制信息"></a>控制信息</h3><p><strong>delay</strong>: 设置响应时间，可以模拟入网环境，如果不设置，那么里面返回数据。</p>
<h2 id="示例"><a href="#示例" class="headerlink" title="示例"></a>示例</h2><p>比如我想Mock接口为<code>http://www.baidu.com</code>一条数据，那么首先启动mms服务。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div></pre></td><td class="code"><pre><div class="line">$ mms</div><div class="line">Path: /usr/<span class="built_in">local</span>/bin/.</div><div class="line">Cmd: mms</div><div class="line">Log level : INFO</div><div class="line"></div><div class="line">          _____                    _____                    _____</div><div class="line">         /\    \                  /\    \                  /\    \</div><div class="line">        /::\____\                /::\____\                /::\    \</div><div class="line">       /::::|   |               /::::|   |               /::::\    \</div><div class="line">      /:::::|   |              /:::::|   |              /::::::\    \</div><div class="line">     /::::::|   |             /::::::|   |             /:::/\:::\    \</div><div class="line">    /:::/|::|   |            /:::/|::|   |            /:::/__\:::\    \</div><div class="line">   /:::/ |::|   |           /:::/ |::|   |            \:::\   \:::\    \</div><div class="line">  /:::/  |::|___|______    /:::/  |::|___|______    ___\:::\   \:::\    \</div><div class="line"> /:::/   |::::::::\    \  /:::/   |::::::::\    \  /\   \:::\   \:::\    \</div><div class="line">/:::/    |:::::::::\____\/:::/    |:::::::::\____\/::\   \:::\   \:::\____\</div><div class="line">\::/    / ~~~~~/:::/    /\::/    / ~~~~~/:::/    /\:::\   \:::\   \::/    /</div><div class="line"> \/____/      /:::/    /  \/____/      /:::/    /  \:::\   \:::\   \/____/</div><div class="line">             /:::/    /               /:::/    /    \:::\   \:::\    \</div><div class="line">            /:::/    /               /:::/    /      \:::\   \:::\____\</div><div class="line">           /:::/    /               /:::/    /        \:::\  /:::/    /</div><div class="line">          /:::/    /               /:::/    /          \:::\/:::/    /</div><div class="line">         /:::/    /               /:::/    /            \::::::/    /</div><div class="line">        /:::/    /               /:::/    /              \::::/    /</div><div class="line">        \::/    /                \::/    /                \::/    /</div><div class="line">         \/____/                  \/____/                  \/____/</div><div class="line"></div><div class="line">[2017-09-20 19:39:45 | INFO] &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;</div><div class="line">[2017-09-20 19:39:45 | INFO] Parse GET : [ [http/https]://www.google.com/[path]/ ] 		 /Users/xiaqiulei/<span class="built_in">test</span>/proxy/./template.groovy -&gt; template</div><div class="line">[2017-09-20 19:39:45 | INFO] &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;</div><div class="line">[2017-09-20 19:39:45 | INFO] Starting proxy at address: 0.0.0.0/0.0.0.0:9090</div><div class="line">[2017-09-20 19:39:45 | INFO] Proxy listening with TCP transport</div><div class="line">[2017-09-20 19:39:46 | INFO] Proxy started at address: /0:0:0:0:0:0:0:0:9090</div></pre></td></tr></table></figure>
<p>添加配置文件<code>baidu.groovy</code>，注意文件后缀必须是<code>.groovy</code>结尾。</p>
<figure class="highlight groovy"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line">mock &#123;</div><div class="line">    desc <span class="string">"baidu"</span></div><div class="line">    request &#123;</div><div class="line">        host <span class="string">"www.baidu.com"</span></div><div class="line">    &#125;</div><div class="line">    response &#123;</div><div class="line">        code <span class="number">200</span></div><div class="line">        headers &#123;</div><div class="line">            header <span class="string">"header_key_1"</span>, <span class="string">"header_value_1"</span></div><div class="line">            header <span class="string">"header_key_2"</span>, <span class="string">"header_value_2"</span></div><div class="line">            header <span class="string">"time"</span>, <span class="keyword">new</span> Date().format(<span class="string">"yyyy-MM-dd HH:mm:ss"</span>)</div><div class="line">        &#125;</div><div class="line">        body &#123;</div><div class="line">            json <span class="string">"""&#123;</span></div><div class="line"><span class="string">    "key":"value",</span></div><div class="line"><span class="string">    "hello":"world"</span></div><div class="line"><span class="string">&#125;"""</span></div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    control &#123;</div><div class="line">        delay <span class="number">2</span></div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>过一会控制台就会出现文件修改的事件。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[2017-09-20 19:40:25 | INFO] &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;</div><div class="line">[2017-09-20 19:40:25 | INFO] Parse X : [ [http/https]://baidu.com/[path]/ ] 		 /Users/xiaqiulei/<span class="built_in">test</span>/proxy/./baidu.groovy -&gt; baidu</div><div class="line">[2017-09-20 19:40:25 | INFO] Parse GET : [ [http/https]://www.google.com/[path]/ ] 		 /Users/xiaqiulei/<span class="built_in">test</span>/proxy/./template.groovy -&gt; template</div><div class="line">[2017-09-20 19:40:25 | INFO] &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;</div></pre></td></tr></table></figure>
<p>然后我们用curl来验证下。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">curl http://www.baidu.com</div><div class="line"></div><div class="line">curl http://www.baidu.com --proxy localhost:9090 -v</div></pre></td></tr></table></figure>
<p><img src="http://upload-images.jianshu.io/upload_images/1520343-63e619ea0a5b37a7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt=""></p>
<p>很显然，两次请求返回的数据是不一样的，只不过第二次我们设置了代理，然后代理返回Mock的数据，同样，在移动端，可以设置的网络代理，然后就可以Mock App中的请求数据。</p>
<h2 id="相关资料"><a href="#相关资料" class="headerlink" title="相关资料"></a>相关资料</h2><p><a href="http://blog.csdn.net/guijiaoba/article/details/43198503" target="_blank" rel="external">利用charles模拟Http请求和响应</a></p>
<p><a href="https://github.com/alibaba/anyproxy" target="_blank" rel="external">Anyproxy Github</a></p>
<p><a href="https://github.com/LiushuiXiaoxia/MyMockServer" target="_blank" rel="external">MyMockServer Github</a></p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechat_pay.jpeg" alt="流水不腐小夏 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.jpeg" alt="流水不腐小夏 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Mock-Server/" rel="tag"># Mock Server</a>
          
            <a href="/tags/MyMockServer/" rel="tag"># MyMockServer</a>
          
            <a href="/tags/Proxy/" rel="tag"># Proxy</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/09/28/MongoDb/" rel="next" title="浅谈MongoDB数据库">
                <i class="fa fa-chevron-left"></i> 浅谈MongoDB数据库
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/10/24/MongoDB-Query/" rel="prev" title="MongoDB查询总结">
                MongoDB查询总结 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/avatar.jpeg"
                alt="流水不腐小夏" />
            
              <p class="site-author-name" itemprop="name">流水不腐小夏</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">31</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">55</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          <div class="links-of-author motion-element">
            
              
                <span class="links-of-author-item">
                  <a href="https://github.com/LiushuiXiaoxia" target="_blank" title="GitHub">
                    
                      <i class="fa fa-fw fa-GitHub"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="http://www.jianshu.com/u/58771f980bac" target="_blank" title="简书">
                    
                      <i class="fa fa-fw fa-简书"></i>简书</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="http://blog.csdn.net/guijiaoba" target="_blank" title="CSDN">
                    
                      <i class="fa fa-fw fa-CSDN"></i>CSDN</a>
                </span>
              
            
          </div>

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#使用MyMockServer模拟数据"><span class="nav-number">1.</span> <span class="nav-text">使用MyMockServer模拟数据</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#简介"><span class="nav-number">1.1.</span> <span class="nav-text">简介</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#安装"><span class="nav-number">1.2.</span> <span class="nav-text">安装</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使用"><span class="nav-number">1.3.</span> <span class="nav-text">使用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#描述"><span class="nav-number">1.3.1.</span> <span class="nav-text">描述</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#请求匹配"><span class="nav-number">1.3.2.</span> <span class="nav-text">请求匹配</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#响应信息"><span class="nav-number">1.3.3.</span> <span class="nav-text">响应信息</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#控制信息"><span class="nav-number">1.3.4.</span> <span class="nav-text">控制信息</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#示例"><span class="nav-number">1.4.</span> <span class="nav-text">示例</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#相关资料"><span class="nav-number">1.5.</span> <span class="nav-text">相关资料</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2015 &mdash; <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">流水不腐小夏</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.3</div>




        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  

  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.3"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.3"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.3"></script>



  


  

    
      <script id="dsq-count-scr" src="https://mycommons-cn.disqus.com/count.js" async></script>
    

    
      <script type="text/javascript">
        var disqus_config = function () {
          this.page.url = 'http://mycommons.cn/2017/10/13/MyMockServer/';
          this.page.identifier = '2017/10/13/MyMockServer/';
          this.page.title = '使用MyMockServer模拟数据';
        };
        var d = document, s = d.createElement('script');
        s.src = 'https://mycommons-cn.disqus.com/embed.js';
        s.setAttribute('data-timestamp', '' + +new Date());
        (d.head || d.body).appendChild(s);
      </script>
    

  




	





  














  




  
  
  
  <link rel="stylesheet" href="/lib/algolia-instant-search/instantsearch.min.css">

  
  
  <script src="/lib/algolia-instant-search/instantsearch.min.js"></script>
  

  <script src="/js/src/algolia-search.js?v=5.1.3"></script>



  

  

  
  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  

</body>
</html>
